﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Modeling_Sub_Pages_NewsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WelcomeUser"] != null)
        {
            if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("ADM"))
            {
            }
            else
            {
                Response.Redirect("~/LoginMessage.aspx?id=1");
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx?id=2");
        }
    }
    
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtDName.Text = "";
        txtDescription.Text = "";
        txtID.Text = "";
        txtlv.Text = "";
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();
        
        string name = txtDName.Text;
        string description = txtDescription.Text;
        string id = txtID.Text;
        int lv = Convert.ToInt32(txtlv.Text);
        if (udm.checkIDSubject(udm.createConnection(), id) != null)
        {
            lblID.Text = "ID not available";
        }
        else 
        {
            if (id.Length == 6 && udm.showSubjectByID(udm.createConnection(), id).Tables[0].Rows.Count == 0)
            {
                if (udm.addSubject(udm.createConnection(), id, name, description, lv))
                {
                    Response.Redirect("MaintainMessage.aspx?ID=10");
                }
                else
                {
                    Response.Redirect("MaintainMessage.aspx?ID=11");
                }
            }
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length == 6)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        string id = txtID.Text;

        if (udm.showSubjectByID(udm.createConnection(), id).Tables[0].Rows.Count > 0)
        {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }
    }
}